/*
 * 收入预算触发器处理
 */
public without sharing class BudgetHandler
{
    ///根据客户编码    匹配客户 和 终端客户
    public static void matchAccount(List<Budget__c> bList)
    {
        Set<String> accCodeSet = new Set<String>();//客户编码
        Set<String> productpkSet = new Set<String>();//产品基本档案pk
        for(Budget__c b:bList){
           accCodeSet.add(b.TerminalCustomerNo__c);
           accCodeSet.add(b.Budget_AccountNo__c);
           productpkSet.add(b.pk_invbasdoc__c);
           
        }
        accCodeSet.add('JKDYKH');
        ///查询客户
        Map<String ,Id> accMap = new Map<String,Id>();
        for(Account a :[select Id,NCAccountNumber__c from Account where NCAccountNumber__c in:accCodeSet]){
        	accMap.put(a.NCAccountNumber__c,a.Id);
        }
        //查询产品
        Map<String,Id> productMap = new Map<String,Id>();
        for(Product__c p:[select Id ,pk_invbasdoc__c from Product__c where pk_invbasdoc__c in:productpkSet]){
        	productMap.put(p.pk_invbasdoc__c,p.Id);
        }
        for(Budget__c b:bList){
        	b.OEM_Account__c = accMap.containsKey(b.TerminalCustomerNo__c) ? accMap.get(b.TerminalCustomerNo__c):accMap.get('JKDYKH'); 
        	if(accMap.containsKey(b.Budget_AccountNo__c))b.custname__c = accMap.get(b.Budget_AccountNo__c); 
            if(productMap.containsKey(b.pk_invbasdoc__c))b.Product__c = productMap.get(b.pk_invbasdoc__c);
        }
    }
}